package com.addcn.pushlibrary.fcm;

import android.content.Intent;
import android.os.PowerManager;
import com.addcn.pushlibrary.LPush;
import com.addcn.pushlibrary.bean.PMessage;
import com.addcn.pushlibrary.utils.catchs.LSharedUtil;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import java.util.Map;

/**
 * author:luwenbo
 * created on date : 2019-05-15
 * content:
 * describe:
 */
public class LFcmService extends FirebaseMessagingService {

    private PowerManager.WakeLock mWakelock;
    private String TAG = "LFcmService";
    @Override
    public void onCreate() {
        super.onCreate();
        if (mWakelock == null) {
            PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);// init powerManager
            mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, "fireTarget:");
        }
        mWakelock.acquire(1000);
    }


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //Log.i(TAG, "onMessageReceived");
        if (remoteMessage.getData().size() > 0) {
            //消息
            if (/* 检查是否需要处理的数据长时间运行的工作 */ true) {
                scheduleJob();
            } else {
                // Handle message within 10 seconds
                handleNow();
            }
        }
        PMessage pMessage = getMessage(remoteMessage);
        LPush.getInstance(this).onMsgReceiver(pMessage,false);
    }

    private PMessage getMessage(RemoteMessage remoteMessage) {
        Map<String,String> data = remoteMessage.getData();
        PMessage pMessage = new PMessage();
        pMessage.setId(data.get("msg_id")==null?"":data.get("msg_id"));
        pMessage.setUrl(data.get("url")==null?"":data.get("url"));
        pMessage.setBigThumb(data.get("image")==null?"":data.get("image"));
        pMessage.setClean(data.get("clean_notification")==null?"":data.get("cleanNotification"));
        if (data.get("title") != null){
            pMessage.setTitle(data.get("title"));
        }else{
            pMessage.setTitle(remoteMessage.getNotification().getTitle());
        }
        if (data.get("body") != null){
            pMessage.setContent(data.get("body"));
        }else{
            pMessage.setContent(remoteMessage.getNotification().getBody());
        }
        pMessage.setFromType(PMessage.FROM_FCM);
        return pMessage;
    }


    public LFcmService() {
        super();
    }


    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
    }

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        LSharedUtil.putString(getApplicationContext(),"l_fcm_token",s);
    }

    /**
     * Handle time allotted to BroadcastReceivers.
     */
    private void handleNow() {
        //Log.d(TAG,"Short lived task is done.");
    }


    /**
     * Schedule a job using FirebaseJobDispatcher.
     */
    private void scheduleJob() {
//        FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
//        Job myJob = dispatcher.newJobBuilder()
//                .setService(TCJobService.class)
//                .setTag("my-job-tag")
//                .build();
//        dispatcher.schedule(myJob);

    }

}
